package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
//import com.sun.org.apache.xpath.internal.operations.Bool;
//import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: leah_ana
 * @Date: 2022/3/31 15:19
 */

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    SetmealDao setmealDao;

    /**
     * 添加检查组 并设置关联的检查
     *
     * @param checkGroup
     * @param ids
     */
    @Override
    public boolean addCheckGroup(CheckGroup checkGroup, Integer[] ids) {
        boolean flag = checkGroupDao.addCheckGroup(checkGroup) > 0;
        Integer checkGroupId = checkGroup.getId();

        this.setCheckGroupAndCheckItem(checkGroupId, ids);

        return flag;
    }

    /**
     * 分页条件模糊查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPageBean(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    /**
     * 根据检查组id查询检查项id集合
     *
     * @param id
     * @return List
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    /**
     * 修改检查组
     *
     * @param checkGroup
     * @param ids
     */
    @Override
    public boolean editCheckGroup(CheckGroup checkGroup, Integer[] ids) {
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleteAssocication(checkGroupId);
        this.setCheckGroupAndCheckItem(checkGroupId, ids);
        return checkGroupDao.edit(checkGroup)>0;

    }

    /**
     * 查询所有检查组
     *
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * 根据id删除检查组
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteCheckGroup(Integer id) {

        int count = checkGroupDao.findSetmealCountByGroupId(id);
        if (count > 0) {
            return false;
        } else {
            checkGroupDao.deleteAssocication(id);
            checkGroupDao.deleteById(id);
        }

        return true;
    }

    /**
     * 设置检查组套餐绑定的检查项
     *
     * @param checkGroupId
     * @param checkitemIds
     */
    @Override
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
